package com.cl.reptile.controller;

import com.cl.reptile.pojo.User;
import com.cl.reptile.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;

/**
 * @author chenlong
 * @create 2021-18-14 14:18
 *
 * 彼岸网图片下载 https://pic.netbian.com/
 *
 * 图片存放路径  D:\图片\reptile
 *
 */
@Controller
@RequestMapping("/biAn")
@Api(tags = "彼岸网")
@Slf4j
public class BiAnPhotoController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/test/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("连接数据库测试")
    public User userInfo(@PathVariable("id") Long id) throws Exception {
        User user = userService.getUserById(id);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(user);
        if (log.isDebugEnabled()){
            log.info("序列化之后：{}",s);
        }
        User readValue = objectMapper.readValue(s,User.class);
        if (log.isDebugEnabled()){
            log.info("反序列化之后:{}",readValue);
        }
        return user;
    }


    @RequestMapping(value = "/downPhoto",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("图片下载：D:\\图片\\reptile")
    public void downPhoto() throws Exception {
        for (int page = 2; page < 1283; page++) {
            String url = "https://pic.netbian.com/4kdongman/";
            Document document = Jsoup.parse(new URL(url), 30000);
            Element element = document.getElementById("main");
            Elements lis = element.getElementsByTag("li");
            int id = 1;
            for (Element n:lis) {
                //图片链接进入第二个页面（document）
                String document2Path = "https://pic.netbian.com/" + n.getElementsByTag("a").get(0).attr("href");
                Document imgDocument = Jsoup.parse(new URL(document2Path), 30000);
                Element img = imgDocument.getElementById("img");
                //图片地址
                String imgUrl = "https://pic.netbian.com" + img.getElementsByTag("img").get(0).attr("src");
                //获取图片名称
                String imgName = img.getElementsByTag("img").get(0).attr("alt");
                try {
                    URL target = new URL(imgUrl);
                    URLConnection urlConnection = target.openConnection();
                    InputStream inputStream = urlConnection.getInputStream();
                    OutputStream outputStream = new FileOutputStream("D:\\图片\\reptile\\"+page+"_"+id+"_"+imgName+".jpg");
                    int temp = 0;
                    while ((temp = inputStream.read()) != -1) {
                        outputStream.write(temp);
                    }
                    System.out.println("第"+ page + "页，第" + id +"张图片下载完毕！");
                    id++;
                    inputStream.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
